package com.bilibili.bilibili.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bilibili.bilibili.R;
import com.bilibili.bilibili.activity.Activitybangumi;
import com.bilibili.bilibili.bean.BangumiBean;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者： 张贤伟
 * 时间： 2017/3/11 14:42
 * 类描述：
 */
public class Bangumimothadapter extends RecyclerView.Adapter<Bangumimothadapter.ViewHolder> {
    private Context context;


    public Bangumimothadapter(Context context) {
        this.context = context;
    }

    private List<BangumiBean.ResultEntity.PreviousEntity> list;

    public void initdata(List<BangumiBean.ResultEntity.PreviousEntity> list) {
        this.list = list;
        this.notifyDataSetChanged();

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.bangumigridview, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.gridView.setAdapter(new Grdiviewadapter2(context, list.get(position).getList()));

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        GridView gridView;

        public ViewHolder(View itemView) {
            super(itemView);
            gridView = (GridView) itemView.findViewById(R.id.bangumigridview);
        }
    }
}

class Grdiviewadapter2 extends BaseAdapter {
    private Context context;
    private List<BangumiBean.ResultEntity.PreviousEntity.ListEntity> list = new ArrayList<>();

    public Grdiviewadapter2(Context context, List<BangumiBean.ResultEntity.PreviousEntity.ListEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder v = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.banguppen_recycleitem, null);
            v = new ViewHolder();
            v.name = (TextView) convertView.findViewById(R.id.banrecycle_name);
            v.imageView = (ImageView) convertView.findViewById(R.id.banrecycleitem_image);
            v.linearLayout = (LinearLayout) convertView.findViewById(R.id.banguppen_lin);
            convertView.setTag(v);
        } else {
            v = (ViewHolder) convertView.getTag();
        }
        v.name.setText(list.get(position).getTitle());
        Glide.with(context).load(list.get(position).getCover()).into(v.imageView);

        v.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Activitybangumi.class);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        private TextView name;
        private ImageView imageView;
        private LinearLayout linearLayout;

    }
}